﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Googler.DAL;
namespace Googler.BLL
{
    public static class Sessions
    {
        public static string SiteLastUpdatedAt
        {
            get
            {
                return Convert.ToString(HttpContext.Current.Application["SiteLastUpdatedAt"]);

            }
            set
            {
                HttpContext.Current.Application["SiteLastUpdatedAt"] = value;
            }
        }
        public static int AggregationTimerPeriod
        {
            get
            {

                string period = Convert.ToString(HttpContext.Current.Application["AggregationTimerPeriod"]);
                if (string.IsNullOrEmpty(period))
                {
                    var context = new DataContext();
                    var appSetting = context.AppSettings.Where(ax => ax.Key == "AggregationTimerPeriod").FirstOrDefault();
                    if (appSetting == null)
                    {
                        period = "30";
                    }
                    period = appSetting.Value;
                }
                return Convert.ToInt32(period);

            }
            set
            {
                HttpContext.Current.Application["AggregationTimerPeriod"] = value;
                var context = new DataContext();
                var appSetting = context.AppSettings.Where(ax => ax.Key == "AggregationTimerPeriod").FirstOrDefault();
                if (appSetting != null)
                {
                    appSetting.Value = value.ToString();
                    context.SaveChanges();
                }
            }
        }
        public static DateTime AggregationLastUpdate
        {
            get
            {
                string date = Convert.ToString(HttpContext.Current.Application["AggregationLastUpdate"]);
                if (string.IsNullOrEmpty(date))
                {
                    var context = new DataContext();
                    var appSetting = context.AppSettings.Where(aps => aps.Key == "AggregationLastUpdate").FirstOrDefault();
                    if (appSetting != null)
                    {
                        date = appSetting.Value;
                        context.SaveChanges();
                    }
                }
                return Convert.ToDateTime(date);

            }
            set
            {
                HttpContext.Current.Application["AggregationLastUpdate"] = value;
                var context = new DataContext();
                var appSetting = context.AppSettings.Where(ax => ax.Key == "AggregationLastUpdate").FirstOrDefault();
                if (appSetting != null)
                {
                    appSetting.Value = value.ToString();
                    context.SaveChanges();
                }
            }
        }
        public static DateTime AppStartedAt
        {
            get
            {
                
                return Convert.ToDateTime( HttpContext.Current.Application["AppStartedAt"]);
            }
            set
            {
                HttpContext.Current.Application["AppStartedAt"] = value;
            }

        }
        public static List<NotificationMessage> NotificationMessages
        {
            get
            {
                var timeLoggings = HttpContext.Current.Application["NotificationMessages"] as List<NotificationMessage>;
                if (timeLoggings == null)
                {
                    timeLoggings = new List<NotificationMessage>();
                    HttpContext.Current.Application["NotificationMessages"] = timeLoggings;
                }
                return HttpContext.Current.Application["NotificationMessages"] as List<NotificationMessage>;
            }
            set
            {
                HttpContext.Current.Application["NotificationMessages"] = value;
            }

        }
        /// <summary>
        /// Application level
        /// </summary>
        public static Dictionary<string, DateTime> AggregatedUrls
        {
            get
            {
                var urlsDic = HttpContext.Current.Application["AggregatedUrls"] as Dictionary<string, DateTime>;
                if (urlsDic == null)
                {
                    HttpContext.Current.Application["AggregatedUrls"] = new Dictionary<string, DateTime>();
                }

                return HttpContext.Current.Application["AggregatedUrls"] as Dictionary<string,DateTime>;
            }
        }


        private static void ReadSiteLastUpdatedAt()
        {
            //string path = Server.MapPath("~/BuildInfo.txt");

            //var fs = File.Create(path);
            //var sw = new StreamWriter(fs);
            //try
            //{
            //    string lastUpdated = DateTime.Now.ToString();
            //    sw.Write(lastUpdated);
            //    Sessions.SiteLastUpdatedAt = lastUpdated;

            //}
            //finally
            //{
            //    sw.Close();
            //    fs.Close();
            //}
        }
    }
}